Passed
Pull Request — master (#5)
by Jesús
01:51
created

grid.ts ➔ createGrid   A

Complexity

Conditions 3

Size

Total Lines 32
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 32
rs 9.184
c 0
b 0
f 0
cc 3
1
import { toggleBox } from '../core/state';
2
import { elements } from '../core/dom';
3
import { updateDisplay } from './display';
4
import { showDisabledBoxToast } from './toast';
5
6
export function createGrid(): void {
7
  elements.grid.innerHTML = '';
8
9
  for (let i = 0; i < 12; i++) {
10
    const box = document.createElement('button');
11
    box.className = 'box';
12
    box.dataset.index = i.toString();
13
    box.type = 'button';
14
15
    const bitValue = Math.pow(2, 11 - i);
16
    box.setAttribute('aria-pressed', 'false');
17
    box.setAttribute('aria-label', `Bit ${i + 1}, value ${bitValue}`);
18
19
    const label = document.createElement('span');
20
    label.className = 'bit-label';
21
    label.textContent = bitValue.toString();
22
    label.setAttribute('aria-hidden', 'true');
23
    box.appendChild(label);
24
25
    box.addEventListener('click', () => {
26
      if (box.dataset.isDisabled === 'true') {
27
        showDisabledBoxToast();
28
        return;
29
      }
30
      toggleBox(i);
31
      updateDisplay();
32
    });
33
34
    elements.grid.appendChild(box);
35
  }
36
37
  updateDisplay();
38
}
39